package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.Course;
import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.bean.SchOrg;
import nsu.edu.zsq.bean.TchCourse;
import nsu.edu.zsq.bean.Teacher;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.param.TchCourseParam;
import nsu.edu.zsq.service.sys.SchOrgService;
import nsu.edu.zsq.service.sys.TeacherCourseService;

/** 
* Description: 教师课程的controller类<br>
*/
@RequestMapping("/sys/teacher/course")
@Controller
public class TeacherCourseController {

    @Autowired
    private TeacherCourseService teacherCourseService;
    @Autowired
    private SchOrgService schOrgService;
    
    /**
     * 获取学校院系的树结构数据以及院系列表
     * @return JsonData
     */
    @RequestMapping("/listSchOrgWithTree")
    @ResponseBody
    public JsonData listSchOrgWithTree() {
        String listSchOrgWithTree = schOrgService.listSchOrgParamWithTree();
        List<SchOrg> listSchOrg = schOrgService.listSchOrg();
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("listSchOrgWithTree", listSchOrgWithTree);
        map.put("listSchOrg", listSchOrg);
        return JsonData.success(map);
    }
    
    /**
     * 根据院系id获取教师的分页数据
     * @param orgId 院系id
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listTeacher")
    @ResponseBody
    public JsonData listTeacherWithPage(@RequestParam("orgId") int orgId, PageQuery pageQuery) {
        PageInfo<Teacher> listTeacher = teacherCourseService.listTeacherWithPage(orgId, pageQuery);
        return JsonData.success(listTeacher);
    }
    
    
    // ======================================课程管理===========================================
    
    /**
     * 跳转到教师课程管理页面
     * @param teacherNo 教师编号
     * @param orgId 院系id
     * @param model
     * @return String manager/teacher/course/teaCourse_manager.html
     */
    @RequestMapping("/toCourseManager")
    public String toCourseManager(@RequestParam("teacherNo")String teacherNo, @RequestParam("orgId")int orgId, Model model) {
        model.addAttribute("teacherNo", teacherNo);
        model.addAttribute("orgId", orgId);
        return "manager/teacher/course/teaCourse_manager.html";
    }
    
    /**
     * 根据教师编号获取该教师课程列表
     * @param teacherNo 教师编号
     * @return JsonData
     */
    @RequestMapping("/listTeacherCourse")
    @ResponseBody
    public JsonData listTeacherCourse(@RequestParam("teacherNo")String teacherNo) {
        List<TchCourseParam> listTchCourse = teacherCourseService.listTchCourse(teacherNo);
        return JsonData.success(listTchCourse);
    }
    
    /**
     * 跳转到选修课的设置页面
     * @param tchCourseId 教师课程id
     * @param model
     * @return String manager/teacher/course/teaCourse_time_edit.html
     */
    @RequestMapping("/toCourseTimeEdit")
    public String toCourseTimeEdit(@RequestParam("tchCourseId")int tchCourseId, Model model) {
        TchCourse tchCourse = teacherCourseService.getTchCourse(tchCourseId);
        model.addAttribute("tchCourse", tchCourse);
        return "manager/teacher/course/teaCourse_time_edit.html";
    }
    
    /**
     * 更新一条教师课程信息
     * @param tchCourse 教师课程信息
     * @return JsonData
     */
    @RequestMapping("/updateTchCourse")
    @ResponseBody
    public JsonData updateTchCourse(TchCourse tchCourse) {
        teacherCourseService.updateTchCourse(tchCourse);
        return JsonData.success();
    }
    
    /**
     * 跳转到课程管理-添加课程页面
     * @param teacherNo 教师编号
     * @param orgId 院系id
     * @param model
     * @return String manager/teacher/course/teaCourse_manager_addCourse.html
     */
    @RequestMapping("/toAddCourse")
    public String toAddCourse(@RequestParam("teacherNo")String teacherNo, @RequestParam("orgId")int orgId, Model model) {
        model.addAttribute("teacherNo", teacherNo);
        model.addAttribute("orgId", orgId);
        return "manager/teacher/course/teaCourse_manager_addCourse.html";
    }
    
    /**
     * 根据院系id获取该院系下所有专业列表
     * @param orgId 院系id
     * @return JsonData
     */
    @RequestMapping("/listMajorByOrgId")
    @ResponseBody
    public JsonData listMajorByOrgId(@RequestParam("orgId")int orgId) {
        List<SchMajor> listMajor = teacherCourseService.listMajorByOrgId(orgId);
        return JsonData.success(listMajor);
    }
    
    /**
     * 获取院系下指定专业的课程列表
     * @param orgId 院系id
     * @param majorNo 专业代码
     * @return JsonData
     */
    @RequestMapping("/listCourseByMajorNo")
    @ResponseBody
    public JsonData listCourseByMajorNo(@RequestParam("orgId")int orgId, @RequestParam("majorNo")String majorNo, PageQuery pageQuery) {
        PageInfo<Course> listCourse = teacherCourseService.listCourseByMajorNo(orgId, majorNo, pageQuery);
        return JsonData.success(listCourse);
    }
    
    /**
     * 单个或者批量添加课程给教师
     * @param courseNo 专业代码数组
     * @return JsonData
     */
    @RequestMapping("/addCourseToTeacher")
    @ResponseBody
    public JsonData addCourseToTeacher(@RequestParam("courseNo[]")String courseNo[],@RequestParam("teacherNo")String teacherNo) {
        if (courseNo.length > 1) {
            // 批量添加
            teacherCourseService.saveBatchCourseToTeacher(courseNo, teacherNo);
        } else if (courseNo.length == 1) {
            // 单个添加
            teacherCourseService.saveCourseToTeacher(courseNo[0], teacherNo);
        } else {
            throw new ParamException("courseNo无参数");
        }
        return JsonData.success();
    }
    
    /**
     * 单个或者批量删除教师课程
     * @param tchCourseId 教师课程id
     * @return JsonData
     */
    @RequestMapping("/delTeacherCourse")
    @ResponseBody
    public JsonData delTeacherCourse(@RequestParam("tchCourseId[]")int tchCourseId[]) {
        if (tchCourseId.length > 1) {
            // 批量删除
            teacherCourseService.deleteBatchTchCourse(tchCourseId);
        } else if (tchCourseId.length == 1) {
            // 单个删除
            teacherCourseService.deleteTchCourse(tchCourseId[0]);
        } else {
            throw new ParamException("tchCourseId无参数");
        }
        return JsonData.success();
    }
    
    // ======================================课表安排===========================================
    
    /**
     * 跳转到‘课表’页面
     * @param teacherNo 教师编号
     * @param model
     * @return String manager/teacher/course/teaCourse_time.html
     */
    @RequestMapping("/toCourseTime")
    public String toCourseTime(@RequestParam("teacherNo")String teacherNo, Model model) {
        List<TchCourseParam> listTchCourse = teacherCourseService.listTchCourseWithCourseTime(teacherNo);
        model.addAttribute("listTchCourse", listTchCourse);
        return "manager/teacher/course/teaCourse_time.html";
    }
    
}
